Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming


Inheritance

Code reuse is one of the great benefits of object-oriented programming. Procedural programming provides code reuse to a certain degree—you can write a procedure and then use it as many times as you want. However, object-oriented programming goes an important step further by allowing you to define relationships between classes. These relationships facilitate code reuse as well as better overall design, by organizing classes and factoring out common elements of related classes. Inheritance is one means of providing this functionality.

Inheritance allows a class to inherit the data members and methods from another class. If one class explicitly inherits from another, the inherited class is the super class of the class that inherits from it. Conversely, any class that inherits from a super class is a subclass of the specified super class. Both a given class and any of its super classes can provide data members and methods for the class definition.

One of the major design goals in object-oriented programming is to factor out common behavior of various classes and separate the common behavior into a super class. This super class contains all the data members and methods that are common to the classes that inherit from it. This inheritance relationship is defined at compile-time and any modifications to a super class are automatically propagated to the subclass. In other words, inheritance creates a strong, static coupling between a super class and its subclasses.

An interface, which is also a type of class, specifies a set of method prototypes and related data definitions that must be defined by any class that implements the interface. Interfaces are thus a way to ensure that a set of classes all implement the same set of methods so that the specified behavior of the entire set of classes can be invoked in a common manner. The method prototypes in an interface simply define the method names and their signatures, but do not contain any method implementation. Also, interfaces do not themselves participate in any inheritance relationships among classes; they only specify method prototypes for a given class definition that implements them. Different classes that implement the interface can provide entirely different implementations for the methods, as long as all the methods in the interface are implemented and have the correct signatures.

Note: In the Progress 4GL, an interface can also specify temp-table and ProDataSet definitions that are passed as parameters to methods that it specifies.

A subclass can override the behavior of its super class by providing a method with the same signature but different behavior from the super class. The overriding method can access the behavior in the super class method, augmenting the super class behavior with pre- or post-processing. This pre- or post-processing contains the subclass’s own unique behavior for the overriding method.

The Progress 4GL, like Java and some other object-oriented languages, uses a single-inheritance model. This model allows a subclass to explicitly inherit from only a single super class. This single super class can then inherit from a single super class, and so on. Thus, a subclass explicitly inherits from its single, immediate super class and implicitly inherits from any single-inherited super classes above it in the class hierarchy. The very top of the class hierarchy is the root class, which is the super class that all subclasses implicitly inherit. Progress provides a built-in root class, Progess.Lang.Object (see the "Using the root class — Progress.Lang.Object" section). A subclass and its class hierarchy can also implement one or more interfaces that the subclass explicitly specifies. As already note, interfaces do not inherit from any other class, nor can they, themselves, be inherited by another class.

Comparison with procedure-based programming

You can also define a form of inheritance with procedure-based objects using super procedures. A set of related super procedures can act in much the same way as a class hierarchy. One internal procedure can invoke another internal procedure of the same name in its super procedure with the RUN SUPER statement, or similarly for user-defined functions using the SUPER built-in function. The key difference between the use of super procedures and a class hierarchy is that the super procedures are related only at run time. Progress has no way to anticipate or validate the interactions between the procedures, and must search the procedure stack to determine if a “super” version of a procedure exists, and where. By contrast, the compiler can validate every reference from one class to another in the same hierarchy, which can then be established reliably at run time.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095